﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using OpenSocialPlugin.General;
using RssToolkit.Rss;

namespace OpenSocialPlugin.Providers
{
  public class Photobucke : BaseSite, ISocialSite
  {

    public Photobucke()
    {
      _capabilities = new List<CapabilitiesEnum>();
      _capabilities.Add(CapabilitiesEnum.HasPhotos);
      _capabilities.Add(CapabilitiesEnum.HasExtras);
      _capabilities.Add(CapabilitiesEnum.CanSearch);
    }

    public List<OSP_Person> GetFriends(OSP_Person person)
    {
      throw new NotImplementedException();
    }

    public List<OSP_Album> GetAlbums(OSP_Person person)
    {
      return FeedToAlbum("http://feed.photobucket.com/groupcategories/Featured/feed.rss");
    }

    private List<OSP_Album> FeedToAlbum(string rssurl)
    {
      RssDocument doc = RssDocument.Load(GetWebData(rssurl));
      return FeedToAlbum(doc);
    }

    private List<OSP_Album> FeedToAlbum(RssDocument doc)
    {
      List<OSP_Album> resp = new List<OSP_Album>();
      foreach (var rssItem in doc.Channel.Items)
      {
        Regex regexObj = new Regex("<img src=\"(.*?)\"", RegexOptions.Singleline);
        resp.Add(new OSP_Album()
        {
          Id =
            rssItem.Link.Replace("http://photobucket.com/groups", "http://feed.photobucket.com/groups") +
            "/feed.rss",
          Thumb = regexObj.Match(rssItem.Description).Groups[1].Value,
          Title = rssItem.Title
        });
      }
     return resp;
    }

    public List<OSP_Photo> GetPhotos(OSP_Album album)
    {
      RssDocument doc = RssDocument.Load(BaseSite.GetWebData(album.Id));
      return FeedToPhotos(doc,album.Id);
    }

    public List<OSP_Photo> FeedToPhotos(RssDocument doc,string albumid)
    {
      List<OSP_Photo> resp = new List<OSP_Photo>();

      foreach (var rssItem in doc.Channel.Items)
      {
        resp.Add(new OSP_Photo()
        {
          Title = rssItem.Title,
          AlbumId = albumid,
          Id = rssItem.Title,
          Thumb = rssItem.MediaContents[0].MediaThumbnails[0].Url,
          Url = rssItem.Enclosure.Url
        });
      }
      return resp;
      
    }

    public GeneralResponse Home()
    {
      GeneralResponse res = new GeneralResponse();
      res.Response = GetAlbums(new OSP_Person());
      return res;
    }

    public GeneralResponse Search(string searchtxt, int type)
    {
      GeneralResponse res = new GeneralResponse();
      RssDocument doc;
      switch (type)
      {
        case 0:
          {
            doc =
              RssDocument.Load(
                GetWebData(string.Format("http://feed.photobucket.com/images/{0}/feed.rss", searchtxt)));
            res.Title = doc.Channel.Title;
            res.Response = FeedToPhotos(doc, "");
          }
          break;
        case 1:
          doc =
            RssDocument.Load(
              GetWebData(string.Format("http://feed.photobucket.com/groupalbums/{0}/feed.rss", searchtxt)));
          res.Title = doc.Channel.Title;
          res.Response = FeedToAlbum(doc);
          break;
          default:
          break;
      }
      return res;
    }

    public GeneralResponse Extras(string searchtxt, int type)
    {
      GeneralResponse res = new GeneralResponse();
      RssDocument doc;
      switch (type)
      {
        case 0:
          doc =
            RssDocument.Load(
              GetWebData("http://feed.photobucket.com/groupcategories/Featured/feed.rss"));
          res.Title = doc.Channel.Title;
          res.Response = FeedToAlbum(doc);
          break;
        case 1:
          doc =
            RssDocument.Load(
              GetWebData("http://photobucket.com/groupactive/feed.rss"));
          res.Title = doc.Channel.Title;
          res.Response = FeedToAlbum(doc);
          break;
        case 2:
          doc =
            RssDocument.Load(
              GetWebData("http://photobucket.com/grouprecent/feed.rss"));
          res.Title = doc.Channel.Title;
          res.Response = FeedToAlbum(doc);
          break;
        case 3:
          doc =
            RssDocument.Load(
              GetWebData("http://photobucket.com/groupcontributers/feed.rss"));
          res.Title = doc.Channel.Title;
          res.Response = FeedToAlbum(doc);
          break;
        case 4:
          doc =
            RssDocument.Load(
              GetWebData("http://photobucket.com/groupcontests/feed.rss"));
          res.Title = doc.Channel.Title;
          res.Response = FeedToAlbum(doc);
          break;
        case 5:
          doc =
            RssDocument.Load(
              GetWebData("http://feed.photobucket.com/recent/images/feed.rss"));
          res.Title = doc.Channel.Title;
          res.Response = FeedToPhotos(doc," ");
          break;
        default:
          break;
      }
      return res;
    }

    public void Init()
    {
      
    }

    public OSP_Person Login()
    {
      return new OSP_Person();
    }

    public Control SettingForm
    {
      get { throw new NotImplementedException(); }
    }

    List<CapabilitiesEnum> _capabilities;
    public List<CapabilitiesEnum> Capabilities { get { return _capabilities; } }

    public OSP_Person GetPerson(string personId)
    {
      throw new NotImplementedException();
    }

    public string GetPersonInfo(OSP_Person person)
    {
      throw new NotImplementedException();
    }

    public string GetPhotoInfo(OSP_Photo photo)
    {
      throw new NotImplementedException();
    }

    public List<string> SearchNames
    {
      get
      {
        List<string> list = new List<string>();
        list.Add("Search photo");
        list.Add("Search groups");
        return list;
      }
    }

    public List<string> ExtrasNames
    {
      get
      {
        List<string> resp = new List<string>();
        resp.Add("Featured albums");
        resp.Add("Most active albums");
        resp.Add("Most recent albums");
        resp.Add("Contributors");
        resp.Add("Contests");
        resp.Add("Most recent photos");
        return resp;
      }
    }

    public override string ToString()
    {
      return "Photobucke";
    }
  }
}
